﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;

namespace McCloudClient
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"";

            // Connect
            McCloud.ServiceClient mccloud = null;
            while (true)
            {
                try
                {
                    mccloud = new McCloud.ServiceClient();
                    Console.Clear();
                    Console.WriteLine("Connected...");
                    Console.WriteLine(mccloud.Endpoint.Address);
                    Console.WriteLine("");
                    break;
                }
                catch
                {
                    Console.Clear();
                    Console.WriteLine("Waiting cloud...");
                    Thread.Sleep(30000);
                }
            }

            // Read number of samples
            double n = 0;
            while(true)
            {
                bool ok = true;
                Console.Write("Enter the number of samples: ");
                string input = Console.ReadLine();
                try{ n = Double.Parse(input); }
                catch{ ok = false; }
                if(n < 0) ok = false;
                if(Math.Truncate(n) < n) ok = false;
                if(ok) break;
                else Console.WriteLine("Invalid input!");
            }
            Console.WriteLine("");
            
            // Read maximum number of samples per task
            double npt = 0;
            while (true)
            {
                bool ok = true;
                Console.Write("Enter the maximum number of samples per task: ");
                string input = Console.ReadLine();
                try { npt = Double.Parse(input); }
                catch { ok = false; }
                if (npt < 0) ok = false;
                if (Math.Truncate(npt) < npt) ok = false;
                if (npt > n) ok = false;
                if (ok) break;
                else Console.WriteLine("Invalid input!");
            }
            Console.WriteLine("");

            // Read codein and codeout
            FileStream fs1 = new FileStream(Path.Combine(path, "codein.txt"), FileMode.OpenOrCreate);
            StreamReader rd1 = new StreamReader(fs1);
            string codein = rd1.ReadToEnd();
            rd1.Close();
            fs1.Close();

            FileStream fs2 = new FileStream(Path.Combine(path, "codeout.txt"), FileMode.OpenOrCreate);
            StreamReader rd2 = new StreamReader(fs2);
            string codeout = rd2.ReadToEnd();
            rd2.Close();
            fs2.Close();

            // Show inputs
            Console.WriteLine("======================= SIMULATION  ======================");
            Console.WriteLine("------------------------- codein -------------------------");
            Console.WriteLine(codein);
            Console.WriteLine("");
            Console.WriteLine("------------------------ codeout -------------------------");
            Console.WriteLine(codeout);
            Console.WriteLine("");
            Console.WriteLine("--------------------------- n ----------------------------");
            Console.WriteLine(n.ToString()+" samples");
            Console.WriteLine("");
            Console.WriteLine("==========================================================");

            // Inicia execução da simulação
            Console.WriteLine("Sending simulation to cloud...");
            string key = mccloud.Run(n, codein, codeout, npt);
            if (key.Substring(0, 5) == "Error")
            {
                Console.WriteLine("Invalid input!");
                Console.WriteLine(key);
            }
            else
            {
                Console.WriteLine("Simulation key: " + key);

                Console.WriteLine("");
                Console.WriteLine("Update status (each 30 seconds). Wait...");                
                while (true)
                {
                    string status = mccloud.Check(key);
                    Console.WriteLine(DateTime.Now.ToLongTimeString() + " - " + status);
                    if (status == "finished") break;
                    Thread.Sleep(30000);
                }

                Console.WriteLine("");
                Console.WriteLine("Downloading...");
                string url = mccloud.Result(key);                
                WebClient webClient = new WebClient();
                string output = DateTime.Now.ToFileTimeUtc().ToString()+".txt";
                webClient.DownloadFile(url, Path.Combine(path, output));
                Console.WriteLine("Results saved in " + output + "!");
            }
            
            Console.WriteLine("");
            Console.Write("Click to close.");
            Console.ReadKey();            
        }
    }
}
